package com.zlzxm.sorting.ui.view.sortview

import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zlzxm.sorting.R

/**
 *
 *CREATE BY zlzxm ON 2022/8/22
 *
 */
class AdapterIndex :BaseQuickAdapter<EntityIndex,BaseViewHolder> (R.layout.item_sort_view_index){

    var selectPosition = -1


    override fun convert(holder: BaseViewHolder, item: EntityIndex) {
        holder.itemView.isSelected = item.isSelect
        holder.setText(R.id.tvTitle,item.title)
    }

    override fun setOnItemClick(v: View, position: Int) {
        if(selectPosition == -1){

            data[position].isSelect = true
            selectPosition = position

            notifyItemChanged(position)

        }else   if(position == selectPosition){

            data[position].isSelect = false
            selectPosition = -1
            notifyItemChanged(position)
        } else{

            data[selectPosition].isSelect = false
            data[position].isSelect = true
            notifyItemChanged(selectPosition)
            notifyItemChanged(position)
            selectPosition = position
        }
        super.setOnItemClick(v, position)
    }


    fun selectPosition(position:Int){

        if(position == -1){

            if(selectPosition != -1){

                data[selectPosition].isSelect = false
                notifyItemChanged(selectPosition)
                selectPosition = -1

            }

        }else{

            if(selectPosition != -1){

                data[selectPosition].isSelect = false
                notifyItemChanged(selectPosition)

            }

            data[position].isSelect = true
            notifyItemChanged(position)
            selectPosition = position

        }

    }

}